package com.ds.lens.data.service;

import com.ds.lens.data.common.asserts.Asserts;
import com.ds.lens.data.common.constant.Constants;
import com.ds.lens.data.service.dao.LensRoleDao;
import com.ds.lens.data.service.dao.LensRoleResourceRelationDao;
import com.ds.lens.data.service.dao.LensUserDao;
import com.ds.lens.data.service.dao.LensUserRoleRelationDao;
import com.ds.lens.data.service.dao.entity.LensApplication;
import com.ds.lens.data.service.dao.entity.LensUser;
import com.ds.lens.data.vo.in.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:
 *
 * @author WeiShaoying
 * @date 2019-11-06
 */
@Slf4j
@Service
public class LensAuthService {

    private static final String USER_PATH_PREFIX = "/lens/user";

    @Autowired
    LensRoleResourceRelationDao lensRoleResourceRelationDao;

    @Autowired
    LensRoleDao lensRoleDao;

    @Autowired
    LensUserDao lensUserDao;

    @Autowired
    LensUserRoleRelationDao lensUserRoleRelationDao;

    @Autowired
    LensApplicationService lensApplicationService;

    public LensUser findLensUserByUserName(String userName) {
        Asserts.assertNotEmpty(userName, Constants.ErrorCode.PARAM_ERROR, "userName can not be empty.");
        return lensUserDao.selectLensUserByUserName(userName);
    }


    public boolean checkPermission(User user, String path, HttpServletRequest request) {
        boolean result = false;
        if (null != user && StringUtils.isNotEmpty(user.getUsername()) && StringUtils.isNotEmpty(path)) {
            if (lensRoleDao.isSysAdmin(Constants.RoleEnum.SYS_ADMIN.getName(), user.getUsername())) {
                user.setAdmin(true);
                request.setAttribute("user", user);
                return true;
            }
            if (lensRoleResourceRelationDao.checkPermission(user.getUsername(), path)) {
                //加载该用户可用的application id 集合
                List<LensApplication> applications = lensApplicationService.findApplicationByUserName(user.getUsername());
                if (CollectionUtils.isNotEmpty(applications)) {
                    user.setApplicationIds(applications.stream().map(LensApplication::getId).collect(Collectors.toList()));
                } else {
                    //非用户类操作 直接返回无权限(原因：用户不属于任何一个application)
                    if (!path.startsWith(USER_PATH_PREFIX)) {
                        return false;
                    }
                }
                request.setAttribute("user", user);
                result = true;
            }
        }
        request.setAttribute("user", user);
        return result;
    }

    public boolean isAdmin(String userName) {
        Asserts.assertNotEmpty(userName, Constants.ErrorCode.PARAM_ERROR, "userName can not be empty.");
        return lensRoleDao.isSysAdmin(Constants.RoleEnum.SYS_ADMIN.getName(), userName);
    }
}
